package com.lagou;

import java.util.concurrent.BlockingQueue;

public class Producer implements Runnable {

    private BlockingQueue<Order> blockingQueue;

    public Producer(BlockingQueue<Order> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        long num = 0;
        while (true) {
            //产生订单
            Order order = new Order();
            order.setId(++num);

            //订单放入队列
            if (blockingQueue.remainingCapacity() > 0) {
                System.out.println(num + "号订单进入队列");
                blockingQueue.add(order);
            } else {
                System.out.println("队列已满，" + num + "号订单被丢弃");
            }

            //每100毫秒产生一个订单
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
